package com.trs.publish.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trs.publish.domain.Result;
import com.trs.publish.util.common.ResultUtil;

/**
 * @author jiahaihong
 *统一异常处理
 */
@ControllerAdvice
public class ExceptionHandle {
	
	private final static Logger logger  = LoggerFactory.getLogger(ExceptionHandle.class);
	
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result handle(Exception e) {
		
		if(e instanceof BusinessException) {
			BusinessException businessException = (BusinessException) e;
			return ResultUtil.error(businessException.getCode(), businessException.getMessage());
		}else {
			logger.info("[系统异常]{}",e);
			return ResultUtil.error(ResultEnum.UNKONW_ERROR.getCode(),ResultEnum.UNKONW_ERROR.getMsg());
		}
	}
}
